<!DOCTYPE html>
<html lang="en">
    <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="format-detection" content="telephone=no" />

  <title>
    16|基础篇怎么理解内存中的Buffer和Cache？ | 迪克猪的博客
  </title>

  
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
  <link rel="manifest" href="/manifest.json" />
  <meta name="theme-color" content="#ffffff" />

  
  <link
    rel="stylesheet"
    href="https://unpkg.com/modern-normalize@0.6.0/modern-normalize.css"
  />

  
  
  
  
  <link rel="stylesheet" href="https://zsy619.github.io/style.min.f7761d111b74dd5c07f0111decee92938c12abc42e0fd319e1a07483e248b54e.css" integrity="sha256-93YdERt03VwH8BEd7O6Sk4wSq8QuD9MZ4aB0g&#43;JItU4=" />

  
  
    
  
</head>

    <body>
        <header id="header">
  <div class="header_container">
    <h1 class="sitetitle">
      <a href="https://zsy619.github.io" title="迪克猪的博客">迪克猪的博客</a>
    </h1>
    <nav class="navbar">
      <ul>
        <li><a href="https://zsy619.github.io">Home</a></li>
        
          <li>
            <a href="/post/">
              
              <span>LINUX性能优化</span>
            </a>
          </li>
        
          <li>
            <a href="/csapp/">
              
              <span>深入理解计算机系统_第三版</span>
            </a>
          </li>
        
          <li>
            <a href="/golang/">
              
              <span>golang</span>
            </a>
          </li>
        
          <li>
            <a href="/docker/">
              
              <span>容器</span>
            </a>
          </li>
        
          <li>
            <a href="/flutter/">
              
              <span>Flutter</span>
            </a>
          </li>
        
          <li>
            <a href="/know/">
              
              <span>知识点</span>
            </a>
          </li>
        
          <li>
            <a href="/categories/">
              
              <span>目录</span>
            </a>
          </li>
        
          <li>
            <a href="/about/">
              
              <span>关于</span>
            </a>
          </li>
        
        <li class="hide-sm"><a href="https://zsy619.github.io/index.xml" type="application/rss+xml">RSS</a></li>
      </ul>
    </nav>
  </div>
</header>

        
<section id="main">
  <article class="post content">
    <h2 class="title">16|基础篇怎么理解内存中的Buffer和Cache？</h2>
    <div class="post_content">
      <h3 id="free数据来源">free数据来源</h3>
<ul>
<li>Buffers 是内核缓冲区用到的内存，对应 /proc/meminfo 中的Buffers值</li>
<li>Cache 是内核页缓存和Slab用到的内存，对应 /proc/meminfo 中Cached与SReclaimable之和</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ watch cat /proc/meminfo
</code></pre></div><p><img src="../../images/20200609-0904-38.png" alt=""></p>
<p>【注】网上的结论可能是对的，但是很可能跟你的环境并不匹配。最简单来说，同一个指标的具体含义，就可能因为内核版本、性能工具版本的不同而有挺大差别。这也是为什么，我总在专栏中强调通用思路和方法，而不是让你死记结论。对于案例实践来说，机器环境就是我们的最大限制。</p>
<h3 id="proc文件系统">proc文件系统</h3>
<ul>
<li>/proc是linux特殊的文件系统，是用户跟内核交互的接口
<ul>
<li>内核的运行状态</li>
<li>配置选项</li>
<li>查询进程的运行状态</li>
<li>统计数据</li>
<li>修改内核配置</li>
</ul>
</li>
<li>也是很多性能工具的最终数据来源
<ul>
<li>free就是通过读取/proc/meminfo，得到内存的使用情况</li>
</ul>
</li>
</ul>
<ul>
<li>Buffers 是对原始磁盘块的临时存储，也就是用来缓存磁盘的数据，通常不会特别大（20MB 左右）。这样，内核就可以把分散的写集中起来，统一优化磁盘的写入，比如可以把多次小的写合并成单次大的写等等。</li>
<li>Cached 是从磁盘读取文件的页缓存，也就是用来缓存从文件读取的数据。这样，下次访问这些文件数据时，就可以直接从内存中快速获取，而不需要再次访问缓慢的磁盘。</li>
<li>SReclaimable 是 Slab 的一部分。Slab 包括两部分，其中的可回收部分，用 SReclaimable 记录；而不可回收部分，用 SUnreclaim 记录。</li>
</ul>
<h3 id="示例">示例</h3>
<p>清理系统缓存：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ echo <span style="color:#ae81ff">3</span> &gt; /proc/sys/vm/drop_caches
</code></pre></div><h4 id="磁盘与文件写入">磁盘与文件写入</h4>
<p>使用vmstat命令</p>
<p><img src="../../images/20200609-0919-44.png" alt=""></p>
<p>【关注重点】内存部分的 buff 和 cache ，以及 io 部分的 bi 和 bo</p>
<ul>
<li>buff 和 cache 就是 Buffers 和 Cache，单位是 KB</li>
<li>bi 和 bo 则分别表示块设备读取和写入的大小，单位为块 / 秒。因为 Linux 中块的大小是 1KB，所以这个单位也就等价于 KB/s</li>
</ul>
<h4 id="场景-1磁盘和文件写案例">场景 1：磁盘和文件写案例</h4>
<p>步骤一：运行dd命令向磁盘分区/dev/sdb1写入2G数据</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">zsy@ubuntu:~$ sudo dd <span style="color:#66d9ef">if</span><span style="color:#f92672">=</span>/dev/urandom of<span style="color:#f92672">=</span>/dev/sdb1 bs<span style="color:#f92672">=</span>1M count<span style="color:#f92672">=</span><span style="color:#ae81ff">2048</span>
dd: error writing <span style="color:#e6db74">&#39;/dev/sdb1&#39;</span>: No space left on device
964+0 records in
963+0 records out
<span style="color:#ae81ff">1009917952</span> bytes <span style="color:#f92672">(</span>1.0 GB, <span style="color:#ae81ff">963</span> MiB<span style="color:#f92672">)</span> copied, 5.31438 s, <span style="color:#ae81ff">190</span> MB/s
</code></pre></div><p>步骤二：观察内存与io变化</p>
<p><img src="../../images/20200609-0945-11.png" alt=""></p>
<h4 id="场景-2磁盘和文件读案例">场景 2：磁盘和文件读案例</h4>
<p>步骤一：读取数据写入空设备</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">zsy@ubuntu:~$ dd <span style="color:#66d9ef">if</span><span style="color:#f92672">=</span>/tmp/file of<span style="color:#f92672">=</span>/dev/null
0+1 records in
0+1 records out
<span style="color:#ae81ff">54</span> bytes copied, 0.000117266 s, <span style="color:#ae81ff">460</span> kB/s
</code></pre></div><p>步骤二：观察内存和 I/O 的变化情况</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">zsy@ubuntu:~$ vmstat <span style="color:#ae81ff">1</span>
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span>  <span style="color:#ae81ff">97304</span>  <span style="color:#ae81ff">20068</span> <span style="color:#ae81ff">985864</span>    <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">165</span>   <span style="color:#ae81ff">178</span>   <span style="color:#ae81ff">182</span>  <span style="color:#ae81ff">249</span>  <span style="color:#ae81ff">524</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">96</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">3</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span>  <span style="color:#ae81ff">97004</span>  <span style="color:#ae81ff">20076</span> <span style="color:#ae81ff">985864</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">12</span> <span style="color:#ae81ff">1056</span> <span style="color:#ae81ff">2413</span>  <span style="color:#ae81ff">4</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">94</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span>  <span style="color:#ae81ff">95728</span>  <span style="color:#ae81ff">20076</span> <span style="color:#ae81ff">985912</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">1590</span> <span style="color:#ae81ff">3150</span> <span style="color:#ae81ff">11</span>  <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">85</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span>  <span style="color:#ae81ff">96508</span>  <span style="color:#ae81ff">20076</span> <span style="color:#ae81ff">985912</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">550</span> <span style="color:#ae81ff">1042</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">96</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span>  <span style="color:#ae81ff">96248</span>  <span style="color:#ae81ff">20076</span> <span style="color:#ae81ff">985912</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">618</span> <span style="color:#ae81ff">1075</span>  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">97</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span>  <span style="color:#ae81ff">96516</span>  <span style="color:#ae81ff">20080</span> <span style="color:#ae81ff">985908</span>    <span style="color:#ae81ff">4</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">16</span>     <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">1308</span> <span style="color:#ae81ff">2007</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">97</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span> <span style="color:#ae81ff">130836</span>  <span style="color:#ae81ff">20088</span> <span style="color:#ae81ff">985756</span>    <span style="color:#ae81ff">8</span>    <span style="color:#ae81ff">0</span>   <span style="color:#ae81ff">900</span>    <span style="color:#ae81ff">48</span>  <span style="color:#ae81ff">863</span> <span style="color:#ae81ff">1750</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">96</span>  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span> <span style="color:#ae81ff">130096</span>  <span style="color:#ae81ff">20088</span> <span style="color:#ae81ff">985756</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">659</span> <span style="color:#ae81ff">1225</span>  <span style="color:#ae81ff">1</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">97</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span> <span style="color:#ae81ff">131104</span>  <span style="color:#ae81ff">20088</span> <span style="color:#ae81ff">985756</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">8</span>  <span style="color:#ae81ff">801</span> <span style="color:#ae81ff">1488</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">96</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
 <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">315136</span> <span style="color:#ae81ff">131088</span>  <span style="color:#ae81ff">20088</span> <span style="color:#ae81ff">985756</span>    <span style="color:#ae81ff">0</span>    <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>     <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">848</span> <span style="color:#ae81ff">1676</span>  <span style="color:#ae81ff">2</span>  <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">97</span>  <span style="color:#ae81ff">0</span>  <span style="color:#ae81ff">0</span>
^Z
<span style="color:#f92672">[</span>6<span style="color:#f92672">]</span>+  Stopped                 vmstat <span style="color:#ae81ff">1</span>
</code></pre></div><p>读磁盘时（也就是 bi 大于 0 时），Buffer 和 Cache 都在增长，但显然 Buffer 的增长快很多。这说明读磁盘时，数据缓存到了 Buffer 中。</p>
<ul>
<li>Buffer 既可以用作“将要写入磁盘数据的缓存”，也可以用作“从磁盘读取数据的缓存”。</li>
<li>Cache 既可以用作“从文件读取数据的页缓存”，也可以用作“写文件的页缓存”。</li>
</ul>
<h3 id="小结">小结</h3>
<ul>
<li>从写的角度来说，不仅可以优化磁盘和文件的写入，对应用程序也有好处，应用程序可以在数据真正落盘前，就返回去做其他工作。</li>
<li>从读的角度来说，既可以加速读取那些需要频繁访问的数据，也降低了频繁 I/O 对磁盘的压力。</li>
</ul>

    </div>
    <div class="info post_meta">
      <time datetime=2020-06-06T16:38:42&#43;0800 class="date">Saturday, June 6, 2020</time>
      
        <ul class="tags">
        
          <li> <a href="https://zsy619.github.io/tags/linux%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96">linux性能优化</a> </li>
        
          <li> <a href="https://zsy619.github.io/tags/linux">linux</a> </li>
        
        </ul>
      
      
    </div>
    <div class="clearfix"></div>
  </article>
  
    <div class="other_posts">
      
      <a href="https://zsy619.github.io/post/15%E5%9F%BA%E7%A1%80%E7%AF%87linux%E5%86%85%E5%AD%98%E6%98%AF%E6%80%8E%E4%B9%88%E5%B7%A5%E4%BD%9C%E7%9A%84/" class="prev">15|基础篇--Linux内存是怎么工作的？</a>
      
      
      <a href="https://zsy619.github.io/post/17%E6%A1%88%E4%BE%8B%E7%AF%87%E5%A6%82%E4%BD%95%E5%88%A9%E7%94%A8%E7%B3%BB%E7%BB%9F%E7%BC%93%E5%AD%98%E4%BC%98%E5%8C%96%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%90%E8%A1%8C%E6%95%88%E7%8E%87/" class="next">17|案例篇--如何利用系统缓存优化程序的运行效率？</a>
      
    </div>
    <aside id="comments">
</aside>

  
</section>

        <a id="back_to_top" title="Go To Top" href="#">
  <span>
    <svg viewBox="0 0 24 24">
      <path fill="none" d="M0 0h24v24H0z"></path>
      <path d="M12 2L4.5 20.29l.71.71L12 18l6.79 3 .71-.71z"></path>
    </svg>
  </span>
</a>

        <footer id="footer">
  <p>
    <span>&copy; 2021 <a href="https://zsy619.github.io" title="迪克猪的博客">迪克猪的博客</a> </span>
    <span>Built with <a rel="nofollow" target="_blank" href="https://gohugo.io">Hugo</a></span>
    <span>Theme by <a rel="nofollow" target="_blank" href="https://github.com/wayjam/hugo-theme-mixedpaper">WayJam</a></span>
  </p>

  <script src="https://zsy619.github.io/js/main.min.8b182175f5874aeed0acc0979345c98d4bde22208ec4f36cc1d6e3102acb4b10.js" integrity="sha256-ixghdfWHSu7QrMCXk0XJjUveIiCOxPNswdbjECrLSxA=" crossorigin="anonymous" async></script>
</footer>

    </body>
</html>
